package com.ruoyi.ldzlsb.service.impl;

import java.util.List;
import java.util.Date;
import java.text.SimpleDateFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.ldzlsb.mapper.SbMaintenanceConfigMapper;
import com.ruoyi.ldzlsb.domain.SbMaintenanceConfig;
import com.ruoyi.ldzlsb.service.ISbMaintenanceConfigService;

/**
 * 保养配置Service业务层处理
 * 
 * @author anli
 * @date 2025-01-11
 */
@Service
public class SbMaintenanceConfigServiceImpl implements ISbMaintenanceConfigService 
{
    @Autowired
    private SbMaintenanceConfigMapper sbMaintenanceConfigMapper;

    /**
     * 查询保养配置
     * 
     * @param configId 保养配置主键
     * @return 保养配置
     */
    @Override
    public SbMaintenanceConfig selectSbMaintenanceConfigByConfigId(Long configId)
    {
        return sbMaintenanceConfigMapper.selectSbMaintenanceConfigByConfigId(configId);
    }

    /**
     * 查询保养配置列表
     * 
     * @param sbMaintenanceConfig 保养配置
     * @return 保养配置
     */
    @Override
    public List<SbMaintenanceConfig> selectSbMaintenanceConfigList(SbMaintenanceConfig sbMaintenanceConfig)
    {
        return sbMaintenanceConfigMapper.selectSbMaintenanceConfigList(sbMaintenanceConfig);
    }

    /**
     * 查询保养配置列表(包含设备类型名称)
     * 
     * @param sbMaintenanceConfig 保养配置
     * @return 保养配置集合
     */
    @Override
    public List<SbMaintenanceConfig> selectSbMaintenanceConfigWithTypeList(SbMaintenanceConfig sbMaintenanceConfig)
    {
        return sbMaintenanceConfigMapper.selectSbMaintenanceConfigWithTypeList(sbMaintenanceConfig);
    }

    /**
     * 新增保养配置
     * 
     * @param sbMaintenanceConfig 保养配置
     * @return 结果
     */
    @Override
    public int insertSbMaintenanceConfig(SbMaintenanceConfig sbMaintenanceConfig)
    {
        // 如果配置编码为空，自动生成
        if (StringUtils.isEmpty(sbMaintenanceConfig.getConfigCode()))
        {
            sbMaintenanceConfig.setConfigCode(generateConfigCode());
        }
        
        if (StringUtils.isNull(sbMaintenanceConfig.getConfigStatus()))
        {
            sbMaintenanceConfig.setConfigStatus("0");
        }
        sbMaintenanceConfig.setIsDelete("0");
        sbMaintenanceConfig.setCreateTime(DateUtils.getNowDate());
        return sbMaintenanceConfigMapper.insertSbMaintenanceConfig(sbMaintenanceConfig);
    }

    /**
     * 修改保养配置
     * 
     * @param sbMaintenanceConfig 保养配置
     * @return 结果
     */
    @Override
    public int updateSbMaintenanceConfig(SbMaintenanceConfig sbMaintenanceConfig)
    {
        sbMaintenanceConfig.setUpdateTime(DateUtils.getNowDate());
        return sbMaintenanceConfigMapper.updateSbMaintenanceConfig(sbMaintenanceConfig);
    }

    /**
     * 批量删除保养配置
     * 
     * @param configIds 需要删除的保养配置主键
     * @return 结果
     */
    @Override
    public int deleteSbMaintenanceConfigByConfigIds(Long[] configIds)
    {
        return sbMaintenanceConfigMapper.deleteSbMaintenanceConfigByConfigIds(configIds);
    }

    /**
     * 删除保养配置信息
     * 
     * @param configId 保养配置主键
     * @return 结果
     */
    @Override
    public int deleteSbMaintenanceConfigByConfigId(Long configId)
    {
        return sbMaintenanceConfigMapper.deleteSbMaintenanceConfigByConfigId(configId);
    }

    /**
     * 校验配置编号是否唯一
     * 
     * @param sbMaintenanceConfig 保养配置
     * @return 结果
     */
    @Override
    public boolean checkConfigCodeUnique(SbMaintenanceConfig sbMaintenanceConfig)
    {
        Long configId = StringUtils.isNull(sbMaintenanceConfig.getConfigId()) ? -1L : sbMaintenanceConfig.getConfigId();
        SbMaintenanceConfig info = sbMaintenanceConfigMapper.selectSbMaintenanceConfigByConfigCode(sbMaintenanceConfig.getConfigCode());
        if (StringUtils.isNotNull(info) && info.getConfigId().longValue() != configId.longValue())
        {
            return false;
        }
        return true;
    }

    /**
     * 校验设备类型是否被保养配置使用
     * 
     * @param typeId 设备类型ID
     * @return 结果 true 存在 false 不存在
     */
    @Override
    public boolean checkTypeIdExistConfig(Long typeId)
    {
        int count = sbMaintenanceConfigMapper.selectCountByTypeId(typeId);
        return count > 0;
    }

    /**
     * 生成配置编码
     * 
     * @return 配置编码
     */
    @Override
    public String generateConfigCode()
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String dateStr = sdf.format(new Date());
        
        // 查询当天已有的配置数量
        SbMaintenanceConfig query = new SbMaintenanceConfig();
        List<SbMaintenanceConfig> configs = sbMaintenanceConfigMapper.selectSbMaintenanceConfigList(query);
        
        // 计算当天的配置数量
        int todayCount = 0;
        for (SbMaintenanceConfig config : configs) {
            if (config.getCreateTime() != null) {
                String configDateStr = sdf.format(config.getCreateTime());
                if (dateStr.equals(configDateStr)) {
                    todayCount++;
                }
            }
        }
        
        int count = todayCount + 1;
        String countStr = String.format("%03d", count);
        
        return "BYPZ" + dateStr + countStr;
    }

    /**
     * 根据配置编码查询保养配置
     * 
     * @param configCode 配置编码
     * @return 保养配置
     */
    @Override
    public SbMaintenanceConfig selectSbMaintenanceConfigByConfigCode(String configCode)
    {
        return sbMaintenanceConfigMapper.selectSbMaintenanceConfigByConfigCode(configCode);
    }

    /**
     * 根据设备类型ID查询保养配置列表
     * 
     * @param typeId 设备类型ID
     * @return 保养配置集合
     */
    @Override
    public List<SbMaintenanceConfig> selectSbMaintenanceConfigByTypeId(Long typeId)
    {
        return sbMaintenanceConfigMapper.selectSbMaintenanceConfigByTypeId(typeId);
    }

    /**
     * 导入保养配置数据
     * 
     * @param configList 保养配置列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importMaintenanceConfig(List<SbMaintenanceConfig> configList, Boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(configList) || configList.size() == 0)
        {
            throw new ServiceException("导入保养配置数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (SbMaintenanceConfig config : configList)
        {
            try
            {
                // 验证是否存在这个保养配置
                SbMaintenanceConfig existConfig = sbMaintenanceConfigMapper.selectSbMaintenanceConfigByConfigCode(config.getConfigCode());
                if (StringUtils.isNull(existConfig))
                {
                    config.setCreateBy(operName);
                    config.setCreateTime(DateUtils.getNowDate());
                    config.setIsDelete("0");
                    this.insertSbMaintenanceConfig(config);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、配置 " + config.getConfigName() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    config.setConfigId(existConfig.getConfigId());
                    config.setUpdateBy(operName);
                    config.setUpdateTime(DateUtils.getNowDate());
                    this.updateSbMaintenanceConfig(config);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、配置 " + config.getConfigName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、配置 " + config.getConfigName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、配置 " + config.getConfigName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
} 